﻿using SpaceNeedle.City6473.Web.Data.Helpers;
using SpaceNeedle.City6473.Web.Data.Models;
using SpaceNeedle.SDK.Web.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpaceNeedle.City6473.Web.Data.Repositories
{
    public class UserDeviceRepository : EntitiyFrameworkRepository<UserDevice>
    {
        public UserDeviceRepository() : base(new City6473Entities())
        {
            this.dataContext.Configuration.ProxyCreationEnabled = false;
        }

        public override UserDevice GetById(Guid id)
        {
            return this.GetAll().Where(x => x.Id == id).SingleOrDefault();
        }

        public List<UserDevice> GetByUser(Guid userId)
        {
            return this.GetAll().Where(x => x.UserId == userId).ToList();
        }

        public UserDevice GetByUserAndPlaftorm(Guid userId, DevicePlatform platform)
        {
            return this.GetAll().Where(x => x.UserId == userId && x.Platform == (int)platform).SingleOrDefault();
        }

        public void NotifyAll(string title, string text, string activationUri)
        {
            try
            {
                foreach (var device in this.GetAll())
                {
                    this.Notify(device, title, text, activationUri);
                }
            }
            catch { }
        }

        public void NotifyByUser(Guid userId, string title, string text, string activationUri)
        {
            try
            {
                foreach (var device in this.GetByUser(userId))
                {
                    this.Notify(device, title, text, activationUri);
                }
            }
            catch { }
        }

        public void NotifyByUserAndPlatform(Guid userId, DevicePlatform platform, string title, string text, string activationUri)
        {
            this.Notify(this.GetByUserAndPlaftorm(userId, platform), title, text, activationUri);
        }

        public bool Notify(UserDevice device, string title, string text, string activationUri)
        {
            if(device == null)
            {
                return false;
            }

            try
            {
                DevicePlatform platform = (DevicePlatform)device.Platform;

                switch (platform)
                {
                    case DevicePlatform.Unknown:
                        break;
                    case DevicePlatform.WP8:
                        PushNotifications.SendWp8ToastNofitication(title, text, activationUri, device.PushId);
                        return true;
                    case DevicePlatform.W8:
                        break;
                    case DevicePlatform.IOS:
                        break;
                    case DevicePlatform.Android:
                        break;
                    default:
                        break;
                }
               
            }
            catch { }

            return false;
        }

        public override UserDevice GetById(string id)
        {
            if(string.IsNullOrEmpty(id))
            {
                return null;
            }

            return this.GetById(new Guid(id));
        }
    }
}
